# -*- coding: utf-8 -*-

import time
import copy
from manager import DownloaderManager, singleton
from downloader.downloader import Downloader
from bdp.i_crawler.i_downloader.ttypes import CrawlStatus

DELIVER_INFO = ['jump_url']

@singleton
class DownloadHandler(object):
    def __init__(self,conf):
        self.conf=conf
        self.manager = DownloaderManager(conf)
        self.downloader = Downloader(conf)
        self.logger = conf.get('log')

    def log(self, request, response):
        log = "status:%s\turl:%s\tdownload_type:%s\telapsed:%s\tproxy_time:%s\thttpcode:%d" \
              % ( response.status,request.url, request.download_type, response.elapsed,request.proxytime,response.http_code)
        if request.proxy!=None:
            log += "\tproxy:%s:%s" % (request.proxy.host, request.proxy.port)       #str(request['proxy']['port']
        if response.status == CrawlStatus.CRAWL_SUCCESS:
            self.logger.info(log)
        else:
            self.logger.error(log)

    def download(self, request):
        if request.retry_times==None:
            retry_times = self.conf.get('default_request_kwargs')['retry_times']
        else:
            retry_times = request.retry_times
        for t in xrange(retry_times):
            req = copy.deepcopy(request)
            self.manager.process_request(req)
            start_time=time.time()
            res = self.downloader.download(req)
            response = self.manager.process_response(req, res)
            req.proxytime = (time.time() - start_time)*1000.0 -response.elapsed
            self.log(req, response)
            if response.status == CrawlStatus.CRAWL_SUCCESS:
                return response
            time.sleep(3)
        return response